package com.mall.shopping.services;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IShoppingService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.PanelConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.dto.PanelListResponse;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @description: IShoppingService接口实现类
 * @author: Woo
 * @create: 2021-08-25 10:44
 **/

@Slf4j
@Service
public class ShoppingServiceImpl implements IShoppingService {

    @Autowired
    PanelConverter converter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;

    /**
     * 返回首页数据list ，包装在PanelListResponse中返回
     * @return
     */
    @Override
    public PanelListResponse homePageShow() {
        PanelListResponse response = new PanelListResponse();
        List<Panel> panels = panelMapper.selectAll();
        try {
            for (Panel panel : panels) {
                List<PanelContentItem> panelContentItems =
                        panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                panel.setPanelContentItems(panelContentItems);
            }
            response.setPanelDtos(converter.panelList2Dto(panels));
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("there are something wrong for homePage show: " + e.getMessage());
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    /**
     * 显示导航栏
     * @return
     */
    @Override
    public NavListResponse navListShow() {
        NavListResponse navListResponse = new NavListResponse();

        try {
            Example example = new Example(PanelContent.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("panelId",GlobalConstants.HEADER_PANEL_ID);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            List<PanelContentDto> panelContentDtos = converter.panelContentList2Dto(panelContents);
            navListResponse.setPannelContentDtos(panelContentDtos);
            navListResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            navListResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("navigation fail to show : " + e.getMessage());
            ExceptionProcessorUtils.wrapperHandlerException(navListResponse,e);
        }
        return navListResponse;
    }


}
